//
//  AlertCustom.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/12/3.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
protocol AlertCustomDelegate {
    func didSelectedBtn(index: NSInteger)
}
class AlertCustom: UIView {
    
    var backView: UIView!
    var alertView: UIView!
    var tipLable: UILabel!
    var title: String!
    var contentStr: String!
    var delegate: AlertCustomDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //创建遮罩
        self.backView = UIView(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
        self.backView.backgroundColor = UIColor.blackColor()
        self.backView.alpha = 0.5
//        let tap = UITapGestureRecognizer(target: self, action: #selector(self.blackClick))
//        self.backView.addGestureRecognizer(tap)
        self.addSubview(backView)
        //创建alert
        self.alertView = UIView(frame: CGRectMake(0, 0, 270, 190))
        self.alertView.center = self.center
        self.alertView.layer.cornerRadius = 17
        self.alertView.clipsToBounds = true
        self.alertView.backgroundColor = UIColor.whiteColor()
        self.addSubview(alertView)
        self.exChangeOut(self.alertView, dur: 0.6)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.tipLable = UILabel(frame: CGRectMake(0, 0, 270, 43))
        self.tipLable.textAlignment = .Center
        self.tipLable.text = title
        tipLable.font = UIFont.systemFontOfSize(18)
    
        tipLable.textColor = UIColor.blackColor()
        self.alertView.addSubview(tipLable)
        self.alertView.frame = CGRectMake(0, 0, 270, 150);
        
        self.creatViewInAlert()
        
        self.alertView.center = CGPointMake(self.center.x, self.center.y);
        
        self.creatBtn()
    }
    
    func creatViewInAlert() {

        let attenL = UILabel(frame: CGRectMake(20, tipLable.frame.origin.y + 10 + 20 , self.alertView.frame.size.width - 40, 50))
        //attenL.backgroundColor = UIColor.redColor()
        attenL.font = UIFont.systemFontOfSize(15)
        attenL.text = contentStr
        attenL.numberOfLines = 0
        attenL.font = UIFont.systemFontOfSize(14)
        attenL.textColor = UIColor.blackColor()
        self.alertView.addSubview(attenL)
        
    }
    func creatBtn() {
       
        
        let lineView:UIView = UIView.init(frame: CGRectMake(0, self.alertView.frame.height - 47, self.alertView.frame.width, 1))
        lineView.backgroundColor = UIColor.lightGrayColor()
        self.alertView.addSubview(lineView)
        
        let middleLineView: UIView = UIView.init(frame: CGRectMake(self.alertView.frame.width / 2, self.alertView.frame.height - 47, 1, 47))
        
        middleLineView.backgroundColor = UIColor.lightGrayColor()
        
        self.alertView.addSubview(middleLineView)
        
        let m:CGFloat = self.alertView.frame.width
        
        for i in 0...1 {
            
            let btn: UIButton = UIButton.init(frame: CGRectMake(20+CGFloat(i) * (20+(m-60)/2), self.alertView.frame.height - 40, (m - 60)/2, 33))
            
            btn.tag = i
            
            if i == 0 {
                btn.setTitle("忽略", forState: UIControlState.Normal)
                btn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
              
                
            } else if i == 1 {
                btn.setTitle("立即查看", forState: UIControlState.Normal)
                btn.setTitleColor(NAVIGATIONBAR_RED_COLOR, forState: UIControlState.Normal)
                
            }
            
            btn.addTarget(self, action: #selector(self.btnClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
            self.alertView.addSubview(btn)
            
           
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func blackClick() {
        self.cancelView()
    }
    
    func btnClicked(sender: UIButton) {
        self.delegate?.didSelectedBtn(sender.tag)
        self.cancelView()
    }
    
    func exChangeOut(changeOutView: UIView, dur: CFTimeInterval) {
        var animation: CAKeyframeAnimation?
        animation = CAKeyframeAnimation(keyPath: "transform")
        animation!.duration = dur
        animation!.removedOnCompletion = false
        animation!.fillMode = kCAFillModeForwards
        var values = [AnyObject]()
        values.append(NSValue(CATransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)))
        values.append(NSValue(CATransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)))
        values.append(NSValue(CATransform3D: CATransform3DMakeScale(0.9, 0.9, 0.9)))
        values.append(NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)))
        animation!.values = values
        
        animation!.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
       
        changeOutView.layer.addAnimation(animation!, forKey: nil)
    }
    
    func cancelView() {
        UIView.animateWithDuration(0.3, animations: {() -> Void in
            self.alpha = 0.0
            }, completion: {(finished: Bool) -> Void in
                self.removeFromSuperview()
                self.alertView = nil
        })

    }
    
    func initWithTitle(title: String!, andContentStr: String!) {
        self.title = title
        self.contentStr = andContentStr
    }
}
